﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using System.Web.UI.HtmlControls;

namespace Golf.WebUI.AdminShow.AttributeManagement
{
    public partial class Golf_ProAttrDetailZSG : Golf.WebUI.App_code.ValidateLogin
    {
        /// <summary>
        /// 产品父类属性
        /// </summary>
        public string attrIDS = "";
        /// <summary>
        /// 是否修改
        /// </summary>
        public string isModify = "";
        /// <summary>
        /// 修改显示的产品属性
        /// </summary>
        public string pro_datelHtml = "";
        /// <summary>
        /// 获取产品的详细属性id
        /// </summary>
        public string pro_datelId = "";
        /// <summary>
        /// 产品id
        /// </summary>
        public string pro_id = "0";
        public int _Id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindAtte();
                switch (Request.Params["action"])
                {
                    case "modify"://修改
                        ShowInfo();
                        break;
                    case "add":
                        _Add();
                        break;
                    case "del":
                        _Del();
                        break;
                    case "save":
                        save();
                        break;
                }
            }
        }

        /// <summary>
        /// 保存  数据
        /// </summary>
        private void save()
        {
            string ProId = Request.Form["pro_id"];
            string AttrId = Request.Form["P_attr_id"];
            string DetaileId = Request.Form["d_attr_id"];
            string _Action = Request.Form["_action"];
            string _ID = Request.Form["_Id"];
            Golf.Model.Golf_ProAttrDetail model = new Golf.Model.Golf_ProAttrDetail();
            model.ProId = int.Parse(ProId);
            model.AttrId = AttrId;
            model.DetaileId = DetaileId;
            Golf.BLL.Golf_ProAttrDetail bll = new Golf.BLL.Golf_ProAttrDetail();
            if (_Action == "add")
            {
                if (GetAttrProid(int.Parse(ProId)))
                    if (bll.Add(model))
                    {
                        Response.Write("添加成功,");
                    }
                    else
                    {
                        Response.Write("添加失败,");
                    }
                else
                    Response.Write("添加失败，改商品已经添加属性 请查看,");
            }
            if (_Action == "modify")
            {
                int Id = int.Parse(_ID);
                model = bll.GetModel(Id);
                model.Id = Id;
                model.ProId = int.Parse(ProId);
                model.AttrId = AttrId;
                model.DetaileId = DetaileId;
                if (bll.Update(model))
                {
                    Response.Write("修改成功,");
                }
                else
                {
                    Response.Write("修改失败,");
                }
            }
        }

        /// <summary>
        /// 绑定父类属性
        /// </summary>
        public void BindAtte()
        {
            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            Repeater1.DataSource = bll.GetList("AttrParent is not NULL and AttrChild is null");
            Repeater1.DataBind();
        }
        /// <summary>
        /// 绑定子类属性
        /// </summary>
        public void BindAtteChild(string ids)
        {
            Golf.BLL.Golf_Attribute bll = new Golf.BLL.Golf_Attribute();
            Repeater2.DataSource = bll.GetList("id in(" + ids + ") and AttrChild is not null and AttrParent is null");
            Repeater2.DataBind();
        }
        /// <summary>
        /// 获取产品属性
        /// </summary>
        /// <param name="id"></param>
        public void GetTypeName(int id)
        {
            Golf.BLL.Golf_ProductType type = new Golf.BLL.Golf_ProductType();
            Golf.Model.Golf_ProductType model = type.GetModel(id);
            if (model != null)
                pro_datelHtml += " 产品类型:<span style='color: Red;'>" + model.TypeName + "</span>&nbsp;&nbsp;";
        }


        /// <summary>
        /// 获取产品品牌
        /// </summary>
        /// <param name="id"></param>
        public void Getpinpai(int id)
        {
            Golf.BLL.Golf_BrandType type = new Golf.BLL.Golf_BrandType();
            Golf.Model.Golf_BrandType model = type.GetModel(id);
            if (model != null)
                pro_datelHtml += " 品牌类型:<span style='color: Red;'>" + model.TypeName + "</span>&nbsp;&nbsp;";
        }
        /// <summary>
        /// 获取产品 相关属性
        /// </summary>
        /// <param name="id"></param>
        public void GetPro(int id)
        {
            Golf.BLL.Golf_Product type = new Golf.BLL.Golf_Product();
            Golf.Model.Golf_Product model = type.GetModel(id);
            if (model != null)
            {
                GetTypeName(model.Pro_TypeId);
                Getpinpai((int)model.Pro_BrandTypeId);
                pro_datelHtml += " 产品名称:<span style='color: Red;'>" + model.Pro_Name + "</span>&nbsp;&nbsp;";
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        private void _Del()
        {
            Golf.BLL.Golf_ProAttrDetail bll = new Golf.BLL.Golf_ProAttrDetail();
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int Id = (Convert.ToInt32(Request.Params["id"]));
                if (bll.Delete(Id))
                {
                    Response.Write("成功删除,");
                }
                else
                {
                    Response.Write("删除失败,");
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void _Add()
        {
            _action.Value = "add";
        }
        private void ShowInfo()
        {
            _action.Value = "modify";
            isModify = "yes";
            int Id = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                Id = (Convert.ToInt32(Request.Params["id"]));
            }
            Golf.BLL.Golf_ProAttrDetail bll = new Golf.BLL.Golf_ProAttrDetail();
            Golf.Model.Golf_ProAttrDetail model = bll.GetModel(Id);
            this.lblId.Text = model.Id.ToString();
            _Id = Id;
            pro_id = model.ProId + "";
            attrIDS = model.AttrId;
            GetPro((int)model.ProId);
            BindAtteChild(model.DetaileId);
            pro_datelId = model.DetaileId;
        }
        public void btnCancle_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('请点击确定取消当前操作！');", true);
        }
        /// <summary>
        /// 根据 商品id查询是否添加属性
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool GetAttrProid(int id)
        {
            Golf.BLL.Golf_ProAttrDetail bll = new Golf.BLL.Golf_ProAttrDetail();
            List<Golf.Model.Golf_ProAttrDetail> list = bll.GetModelList(" ProId=" + id);
            if (list.Count >= 1)
                return false;
            else
                return true;
        }
    }
}
